"""
解析期刊得到卷
"""
import facade
import parsel
from parsel import Selector
from xjlibrary.our_file_dir import BaseDir

curpath = BaseDir.get_file_dir_absolute(__file__)
TopPath = BaseDir.get_upper_dir(curpath, -2)
configfile = BaseDir.get_new_path(curpath, "db.ini")
dirPath = BaseDir.get_new_path(TopPath, "download", "intlpress", "download", "vols")


class ParaJournal(object):

    def __init__(self):
        self.logger = facade.get_streamlogger()
        self.mysqlutils = facade.MysqlUtiles(configfile, "db", logger=self.logger)

    def select(self):
        for file in BaseDir.get_dir_all_files(dirPath):
            filename = BaseDir.get_filename_not_extsep(file)
            html = BaseDir.single_read_file(file)
            selector = Selector(text=html)
            div_list = selector.xpath('//*[@id="list"]/div')
            for div in div_list:
                href = div.xpath('.//a/@href').get()
                voltext = div.xpath('.//a/p/text()').get()
                vol = voltext.split("(")[0].replace("Volume", "").strip()
                year = voltext.split("(")[-1].replace(")", "").strip()
                sql = "replace into vol (url,vol,`year`,jid) values ('%s','%s','%s','%s')" % (
                    href, vol, year, filename)
                self.mysqlutils.ExeSqlToDB(sql)


def main():
    para = ParaJournal()
    para.select()


if __name__ == "__main__":
    main()
